<?php

namespace app\common\library;

use app\common\model\UrlPerson;
use app\common\library\YuUtil;

use think\db;

class YuPerdata
{
    const VERSION = '6.0.5';

    protected $errorString = '';

    public static $instance ;

    /**
     * @title 单例调用
     * @description 单例调用
     * @author Admin ldy
     * @time 2018/9/25  12:22
     */
    public static function getInstance(){
        if (YuPerdata::$instance == null){
            YuPerdata::$instance = new \app\common\library\YuPerdata;
            return YuPerdata::$instance;
        }else{
            return YuPerdata::$instance;
        }
    }

    public function __construct(){
        //初始化
    }

    public function setErrorString($msg){
        $this->errorString = $msg;
    }

    public function getErrorString(){
        return $this->errorString;
    }

    public static function addPersonUrlByType($option,$type = 'fast'){
        $url = $option['url'];
        $typeid = $option['typeid'];
        $nickname = $option['nickname'];
        $desc = $option['desc'];
        $key_word = $option['key_word'];
        $uid = $option['uid'];
        $token = $option['token'];
        $data = [
            'typeid' => $typeid,
            'u'=>$url,
            'nickname'=>$nickname,
            'desc'=>$desc,
            'key_word'=>$key_word,
        ];
        $validate = new \app\common\validate\PerUrl;
        if (!$validate->scene('perAddByU')->check($data)) {
            return new ReFail($validate->getError());
        }
        //先操作增加公共URL数据
        $re = \app\common\library\YuDa::addPublicUrl($url,$token);
        if (!($re instanceof \app\common\library\YuResult)){
            //如果返回的不是这个格式的，报错
            return new ReFail(ErrorType::DATA_EXCEPTION);
        }
        if (!($re->getStatus())){
            //新增公共url操作失败
            return new ReFail(ErrorType::ADD_DATA_FAILED,$re->getData());
        }
        //在操作增加个人的url
        $data['token'] = $token;
        $data['uid'] = $uid;
        if ($type=='normal'){
            $re =  \app\common\library\YuDa::addPersonUrl($url,$token,$data);
            if ($re->getCode()){
                rewardNormal($uid,'新增【链接】成功！');
                return new ReSuccess('新增成功！',$re->getData(),200);
            }else{
                return new ReFail(ErrorType::ADD_DATA_FAILED,$re->getData());
            }
        }else if($type == 'fast'){
            $jobHandlerClassName = 'app\common\job\AddPersonUrl';
            $jobData = ['token' => $token, 'u' => $url,'data'=>json_encode($data)];
            $isPushed = \app\common\library\YuUtil::postMsg($jobHandlerClassName, $jobData);
            if ($isPushed) {
                $pushTime = date('Y-m-d H:i:s') . " a new AddPersonUrl Job is Pushed to the MQ" . "<br>";
                rewardNormal($uid,'新增【链接】成功！');
                return new ReSuccess('新增成功！',$pushTime,200);
            } else {
                $pushTime = 'Oops, something went wrong.';
                return new ReFail(ErrorType::ADD_DATA_FAILED,$pushTime);
            }
        }else{
            //这里可以扩展
            return new ReFail(ErrorType::PARAMS_CAN_NOT_BE_EMPTY);
        }

    }

    public static function getPerDataByUserId($uid,$page){
        $wherePage=array(
            'page' => $page,
        );
        if (!empty($uid)){
            $where = array('a.user_id'=>$uid,'a.status_is_detele'=>0);
        }else{
            $where=array();
        }
        $list = \think\Db::table('scj_url_person')
                ->field('w.url,a.id,a.desc,a.nickname,a.keyword,a.addtime')
                ->alias('a')
                ->join('scj_url_public w','a.base_url_id = w.id')
                ->where($where)
                ->paginate(50,true,$wherePage);
        if ($list){
            return new ReSuccess('获取数据成功！',$list);
        }else{
            return new ReFail(ErrorType::GET_DATA_FAILED);
        }
    }





}
